package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import cn.hutool.core.bean.BeanUtil;
import com.ruicar.afs.cloud.afscase.approveprev.entity.CaseApprovePrevInfo;
import com.ruicar.afs.cloud.afscase.approveprev.service.CaseApprovePrevInfoService;
import com.ruicar.afs.cloud.afscase.mq.sender.ApprovePrevSender;
import com.ruicar.afs.cloud.afscase.risk.service.CaseTortoiseService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ComAttachmentFileDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.approveprev.ApprovePrevInfoDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.approveprev.ApprovePrevResultDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/08/08 11:20
 */
@Component
@AllArgsConstructor
@Slf4j
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit",name="enable")
public class ApprovePrevProcessor implements AfsMqBizProcessor<ApprovePrevInfoDto> {

    /**
     * Case approve prev info service
     */
    private CaseApprovePrevInfoService caseApprovePrevInfoService;
    /**
     * Case tortoise service
     */
    private CaseTortoiseService caseTortoiseService;
    /**
     * Approve prev sender
     */
    private ApprovePrevSender approvePrevSender;

    private ComAttachmentFileService comAttachmentFileService;

    /**
     * Author Peng Jinbo
     * Process message boolean
     *
     * @param prevInfoDto prev info dto
     * @return the boolean
     * @throws Exception exception
     */
    @Override
    @Transactional
    public boolean processMessage(ApprovePrevInfoDto prevInfoDto) throws Exception {
        try {
            CaseApprovePrevInfo prevInfo = savePrev(prevInfoDto);
            caseTortoiseService.signalPrev(prevInfo);
            noticeReceiveStatus(prevInfoDto.getApplyId(),WhetherEnum.YES);
            log.info("预审批消息接收成功");
            return true;
        } catch (Exception e) {
            noticeReceiveStatus(prevInfoDto.getApplyId(),WhetherEnum.NO);
            log.info("预审批信息接收失败", e);
            return false;
        }
    }

    /**
     * Author Peng Jinbo
     * Notice receive status *
     * 通知进件端处理结果
     *
     * @param applyId apply id
     * @param yesOrNo yes or no
     */
    private void noticeReceiveStatus(String applyId, WhetherEnum yesOrNo){
        AfsTransEntity<ApprovePrevResultDto> afsTransEntity = new AfsTransEntity<>();
        afsTransEntity.setTransCode(MqTransCode.AFS_POS_CASE_APPLY_CTM_APPROVE_PREV_NOTICE);
        afsTransEntity.setData(ApprovePrevResultDto.builder()
                .applyId(applyId)
                .receiveStatus(yesOrNo.getCode())
                .build());
        approvePrevSender.noticeReceiveStatus(afsTransEntity);
    }


    /**
     * @Description 保存预审批信息
     * @Author Peng_Jinbo
     * @Date 2020/8/8 14:01
     */
    private CaseApprovePrevInfo savePrev(ApprovePrevInfoDto prevInfoDto) {
        log.info("接收预审批数据 START ...");
        CaseApprovePrevInfo prevInfo = new CaseApprovePrevInfo();
        prevInfo.setApplyId(prevInfoDto.getApplyId());
        prevInfo.setApplyDate(prevInfoDto.getApplyDate());
        prevInfo.setCertNo(prevInfoDto.getCertNo());
        prevInfo.setCertStartDate(prevInfoDto.getCertStartDate());
        prevInfo.setCertEndDate(prevInfoDto.getCertEndDate());
        prevInfo.setCertType(prevInfoDto.getCertType());
        prevInfo.setChannelCode(prevInfoDto.getChannelCode());
        prevInfo.setChannelName(prevInfoDto.getChannelName());
        prevInfo.setCustName(prevInfoDto.getCustomerName());
        prevInfo.setTelPhone(prevInfoDto.getPhone());
        prevInfo.setSellerName(prevInfoDto.getSellerName());
        prevInfo.setSellerRealName(prevInfoDto.getSellerRealName());
        prevInfo.setApplyDate(prevInfoDto.getApplyDate());
        prevInfo.setBusinessType(prevInfoDto.getBusinessType());
        prevInfo.setSex(prevInfoDto.getSex());
        prevInfo.setBirthday(prevInfoDto.getBirthday());
        prevInfo.setSellerPhone(prevInfoDto.getSellerPhone());
        prevInfo.setApplyNo(prevInfoDto.getApplyNo());

        List<ComAttachmentFileDto> fileDtoList = prevInfoDto.getComAttachmentFileDtoList();
        List<ComAttachmentFile> fileList = new ArrayList<>(fileDtoList.size());
        for (ComAttachmentFileDto comAttachmentFileDto : fileDtoList) {
            ComAttachmentFile file = new ComAttachmentFile();
            BeanUtil.copyProperties(comAttachmentFileDto,file);
            file.setBusiNo(prevInfo.getApplyNo());
            file.setBelongNo(prevInfo.getApplyNo());
            fileList.add(file);
        }
        comAttachmentFileService.saveBatch(fileList);
        caseApprovePrevInfoService.save(prevInfo);
        log.info("接收预审批数据 END ...");
        return prevInfo;
    }

    /**
     * @return the code
     * @Author Peng Jinbo Gets code *
     */
    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_PREAPPROVE_INFO_PUSH;
    }


}
